package com.fanghuaiming.ml.order.service.impl;

import com.fanghuaiming.ml.common.model.enumeration.OrderTransactionEnum;
import com.fanghuaiming.ml.order.controller.model.MessageListOrderDTO;
import com.fanghuaiming.ml.order.dao.mapper.MessageListOrderMapper;
import com.fanghuaiming.ml.order.dao.model.MessageListOrder;
import com.fanghuaiming.ml.order.service.MessageListOrderService;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/****
 * @description: 订单服务接口实现类
 * @version:1.0.0
 * @author fanghuaiming
 * @data Created in 2020/11/27 下午1:59
 *
 */
@Service
@Slf4j
public class MessageListOrderServiceImpl implements MessageListOrderService {

    /**
     * 订单dao
     */
    @Autowired
    private MessageListOrderMapper messageListOrderMapper;

    /**
     * @Description: 处理订单
     *
     * 业务:订单已存在但是未支付
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/12/4 下午2:22
     * @version:1.0.0
     */
    @SuppressWarnings("all")
    @Transactional(transactionManager = "messageListOrderTransactionManager")
    @Override
    public OrderTransactionEnum dealOrder(MessageListOrderDTO messageListOrderDTO){
        log.info("====== 订单微服务即将开始进行执行 订单号:{} ======",messageListOrderDTO.getOrderId());
        //查询订单是否已经存在
        MessageListOrder messageListOrder =  messageListOrderMapper.selectByOrderId(messageListOrderDTO.getOrderId());
        if(null == messageListOrder){
            log.info("====== 订单微服务 - 订单不存在,订单号:{} ======",messageListOrderDTO.getOrderId());
            return OrderTransactionEnum.ORDER_IS_NOT_EXIST;
        }
        log.info("====== 订单微服务 - 原订单信息,订单:{} ======",messageListOrder);
        //设置为已支付
        messageListOrder.setOrderStatus(1);
        messageListOrder.setUpdateTime(new Date());
        //代表系统更新
        messageListOrder.setUpdateUser(0L);
        //进行更新
        messageListOrderMapper.updateOrderById(messageListOrder);
        log.info("====== 订单微服务 - 更新原订单信息,更新后订单信息:{} ======",messageListOrder);
        //TODO 进行异步请求订单微服务进行修改或者try/catch捕获，反正不影响本次已经扣款动作,不让本地事务进行回滚
        //更新成功
        return OrderTransactionEnum.SUCCESS;
    }

}
